package com.zq.im.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gyf.immersionbar.ImmersionBar;
import com.zq.im.BaseActivity;
import com.zq.im.R;
import com.zq.im.api.FriendsApi;
import com.zq.im.bean.BaseModel;
import com.zq.im.bean.FriendBean;
import com.zq.im.retrofit.BaseCallBack;
import com.zq.im.retrofit.RetrofitManager;
import com.zq.im.utils.SharePreferenceUtils;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;

public class SearchFriendActivity extends BaseActivity {

    private static final String TAG = "SearchFriendActivity";
    @BindView(R.id.edit_text)
    EditText mEditText;

    @BindView(R.id.search)
    View search;

    @BindView(R.id.content)
    TextView content;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_friend;
    }

    @Override
    protected void initImmersionBar() {
        ImmersionBar.with(this)
                .titleBarMarginTop(R.id.toolbar)
                .barColor(R.color.default_white)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void initView() {
        //打开页面
//        mEditText.setFocusable(true);
//        mEditText.setFocusableInTouchMode(true);
//        mEditText.requestFocus();
//        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Log.d(TAG, "beforeTextChanged" + charSequence.toString());
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Log.d(TAG, "onTextChanged" + charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {
                Log.d(TAG, "afterTextChanged" + editable.toString());
                String s = editable.toString();
                content.setText(s);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.cancel)
    void cancel() {
        finish();
    }

    @OnClick(R.id.search)
    void search() {
        FriendsApi friendsApi = RetrofitManager.getRetrofit().create(FriendsApi.class);
        String userId = SharePreferenceUtils.getString("userId");
        Call<BaseModel<FriendBean>> searchFriendCall = friendsApi.searchFriend(userId, content.getText().toString());
        searchFriendCall.enqueue(new BaseCallBack<FriendBean>() {
            @Override
            public void onSuccess(FriendBean bean) {
                Intent intent = new Intent(mActivity, FriendDetailActivity.class);
                intent.setAction("SearchFriendActivity");
                intent.putExtra("friend", bean);
                startActivity(intent);
            }

            @Override
            public void onFailure(Throwable t) {
                Toast.makeText(mActivity, t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }

}